/**
 * 
 */
package mainWindow;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import DataBase.*;
/**
 * @author AI Wei
 *
 */
@SuppressWarnings("serial")
public class ShowPanel extends JTabbedPane implements ChangeStyle
{
	private GuidePanel guidePanel;
	private HashMap<String, WordShowPanel> wordMap;
	private HomePanel homePanel;
	public void setGuidePanel(GuidePanel guidePanel)
	{
		this.guidePanel = guidePanel;
	}
	public void init()
	{	
		wordMap = new HashMap<String, WordShowPanel>();
		homePanel = new HomePanel();
		this.addTab("HOME", new ImageIcon("images/icons/home.png"), homePanel, null);
		
		this.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e) {
				if (!(getSelectedComponent() instanceof HomePanel)){
					guidePanel.changeWord(((WordShowPanel)getSelectedComponent()));	
				}
				else {
					guidePanel.showIndex();
				}
			}			
		});
		setBackground(null);
		setOpaque(false);
	}
	public ShowPanel(GuidePanel guidePanel){
		super(JTabbedPane.TOP);
		init();
		setGuidePanel(guidePanel);
	}
	public void addWord(Word word)
	{
		WordShowPanel wordShowPanel;
		String wordString = word.getWord();
		if (wordMap.containsKey(wordString))
		{
			wordShowPanel = wordMap.get(wordString);
		}
		else
		{
			wordShowPanel  = new WordShowPanel(guidePanel, word);
			wordShowPanel.init();
			wordMap.put(wordString, wordShowPanel);
			this.addTab(wordString, wordShowPanel);
			try{
				setTabComponentAt(indexOfComponent(wordShowPanel), new Tab(this, wordString));
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		this.setSelectedComponent(wordShowPanel);
		wordShowPanel.showGuide();
	}
	private void removeWord(String wordString)
	{
		wordMap.remove(wordString);
	}
	public void removeIndex(int i)
	{
		Component component = this.getComponentAt(i);
		//removeWord(((WordShowPanel)component).word.getWord());
		remove(i);
	}
	public void showHome()
	{
		this.setSelectedComponent(homePanel);
	}
	
	public void changeStyle(int styleIdx) {
		setBackground(null);
		setOpaque(false);
		homePanel.changeStyle(styleIdx);
		Component[] components = getComponents();
		System.out.println(components.length);
		for (Component i:components)
		{
			if (i instanceof ChangeStyle)
			{
				((ChangeStyle) i).changeStyle(styleIdx);
			}
		}
	}
	/*
	private Image backgroundImage;
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(backgroundImage, 0, 0,this.getWidth(),this.getHeight(),null);
	}
	public void changeStyle(int styleIdx) {
		backgroundImage = StyleTool.getRightImage(styleIdx);
		System.out.println("hello");
		this.repaint();		
	}
	*/
}

class Tab extends JPanel
{
	private ShowPanel showPanel;
	public Tab(final ShowPanel showPanel, String name) {
        //unset default FlowLayout' gaps
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (showPanel == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        this.showPanel = showPanel;
        setOpaque(false);
        
        //make JLabel read titles from JTabbedPane
        JLabel label = new JLabel(name);
        
        add(label);
        //add more space between the label and the button
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        //tab button
        JButton button = new CloseButton();
        add(button);
        //add more space to the top of the component
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }
	private class CloseButton extends JButton implements ActionListener
	{
		public CloseButton()
		{
			int size = 17;
            setPreferredSize(new Dimension(size, size));
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
		}
		public void actionPerformed(ActionEvent e) {
			int i = showPanel.indexOfTabComponent(Tab.this);
            if (i != -1) {
                showPanel.removeIndex(i);
            }
		}
		protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //shift the image for pressed buttons
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g2.setColor(Color.MAGENTA);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
	}
	private static MouseListener buttonMouseListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }
        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
}

class HomePanel extends JPanel implements ChangeStyle
{
	public HomePanel()
	{
		setLayout(new BorderLayout());
		JLabel tLabel = new JLabel("Welcome to LexiGaGa!!");
		tLabel.setFont(new Font("Dialog", Font.BOLD|Font.ITALIC, 30));
		add(tLabel, BorderLayout.NORTH);
		JTextPane tText = new JTextPane();
		try {
			tText.setPage("File:"+new File("data/welcome.html").getAbsolutePath());
		}
		catch (IOException ex){
			ex.printStackTrace();
		}
		tText.setEditable(false);
		tText.setBackground(null);
		tText.setOpaque(false);
		add(tText);		
		
	}
	
	private Image backgroundImage;
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(backgroundImage, 0, 0,this.getWidth(),this.getHeight(),null);
	}
	public void changeStyle(int styleIdx) {
		setBackground(null);
		setOpaque(false);
		backgroundImage = StyleTool.getRightImage(styleIdx);
		this.repaint();		
	}
	
	
}

